import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.*;

import com.jgoodies.forms.layout.*;
/*
 * Created by JFormDesigner on Mon Apr 18 21:12:36 CDT 2011
 */

/**
 * @author Becca Emrick
 */


public class StartGUI extends JFrame {
	//	private JList userList;
	//	private JTextField addUserField;
	//	private JButton addUserButton;
	//	private JButton startButton;

	public StartGUI() {
		initComponents();
		setBounds(400, 600, 400, 300);
		DefaultListModel model = new DefaultListModel();
		//		loadUserList();
		model1 =  new DefaultListModel();
		getUserList().setModel(model1);

		//loadFakeUserList();
		loadUsers();
		System.out.println("really"+model1); 
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void loadFakeUserList() {
		// model = (DefaultListModel) getUserList().getModel();
		//model =  new DefaultListModel();
		model1.addElement("Retire Ski");
		model1.addElement("Crack Beer Mice");
		model1.addElement("Obscurant Liars");
		model1.addElement("Joy When Dr");
		model1.addElement("Jet Inane, Neat Me");
	}
	
	public void loadUsers() {
		String[] users = GameManager.GM.database.getAllUsers();
		
		if( users == null) {
			return;
		}
		for(int i = 0; i < users.length; i++) {
			model1.addElement(users[i]);
		}
	}

	public void startButtonAction(ActionEvent e) {
		if ( userList.getSelectedValue() == null ) {
			return;
		}
		GameManager.GM.userName = (String) userList.getSelectedValue();
		this.setVisible(false);
		GameManager.GM.editor.setVisible(true);

		System.out.println("Open GLWindow");
		GameManager.GM.graphicsEngine.openWindow();
		
	}

	//	public  void loadUserList() {
	//		String[] users = GameManager.GM.database.getAllUsers();
	//		DefaultListModel model = (DefaultListModel) getUserList().getModel();
	//		for(String user: users) {
	//			model.addElement(user);
	//		}
	//		
	//	}

	public void addUserWithButton(ActionEvent e) throws SQLException {
		String name = addUserField.getText().trim();
		if ( name.equals("") ) {
			return;
		}
		System.out.println(name);
		model1.addElement(name);
		GameManager.GM.database.addUser(name);
		System.out.println(model1);
		//addUser();
	}

	public void addNewUser(ActionEvent e) throws SQLException {
		
	}

	public void addUser() {
		DefaultListModel model = (DefaultListModel) getUserList().getModel();
		String text  = addUserField.getText().trim();
		if(!text.equals("")) {
			model.addElement(text);
			System.out.println("Add user to database");
			//GameManager.GM.database.addUser(text);
		}

		addUserField.setText("");
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Becca Emrick
		label1 = new JLabel();
		scrollPane1 = new JScrollPane();
		setUserList(new JList());
		addUserField = new JTextField();
		addUserButton = new JButton();
		startButton = new JButton();

		//======== this ========
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- label1 ----
		label1.setText("Users: ");
		contentPane.add(label1);
		label1.setBounds(new Rectangle(new Point(28, 0), label1.getPreferredSize()));

		//======== scrollPane1 ========
		{

			//---- userList ----
			getUserList().setLayoutOrientation(JList.HORIZONTAL_WRAP);
			scrollPane1.setViewportView(getUserList());
		}
		contentPane.add(scrollPane1);
		scrollPane1.setBounds(28, 20, 306, 151);

		//---- addUserField ----
		addUserField.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					addNewUser(e);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		contentPane.add(addUserField);
		addUserField.setBounds(28, 176, 156, addUserField.getPreferredSize().height);

		//---- addUserButton ----
		addUserButton.setText("Add User");
		addUserButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					addUserWithButton(e);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		contentPane.add(addUserButton);
		addUserButton.setBounds(new Rectangle(new Point(189, 176), addUserButton.getPreferredSize()));

		//---- startButton ----
		startButton.setText("Start");
		startButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				startButtonAction(e);
			}
		});
		contentPane.add(startButton);
		startButton.setBounds(new Rectangle(new Point(291, 176), startButton.getPreferredSize()));

		{ // compute preferred size
			Dimension preferredSize = new Dimension();
			for(int i = 0; i < contentPane.getComponentCount(); i++) {
				Rectangle bounds = contentPane.getComponent(i).getBounds();
				preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
				preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
			}
			Insets insets = contentPane.getInsets();
			preferredSize.width += insets.right;
			preferredSize.height += insets.bottom;
			contentPane.setMinimumSize(preferredSize);
			contentPane.setPreferredSize(preferredSize);
		}
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	public void setUserList(JList userList) {
		this.userList = userList;
	}

	public static JList getUserList() {
		return userList;
	}
	public DefaultListModel getModel() {
		System.out.println(model1);
		return model1;
	}
	public static void main(String [] args) throws ClassNotFoundException, SQLException{
		StartGUI gui=  new StartGUI();
		gui.setSize(1000,1000);
		gui.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		gui.setVisible(true);
	}


	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Becca Emrick
	private JLabel label1;
	private JScrollPane scrollPane1;
	static JList userList;
	private JTextField addUserField;
	private JButton addUserButton;
	private JButton startButton;
	static DefaultListModel model1;
	//private String 
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
